import Axios from 'axios';
import { Message } from 'view-design'
import cookieUse from '@/libs/cookie.js';
import baseUrl from '@/api/index';
import router from '@/router';
import qs from 'qs';
import { escapeRequestArgument, escapeResponseArgument } from '@/libs/utils';

// 复制的是 axios1 的内容啊啊啊
// 所以如果原来是 axios的话 要改返回的判断

sessionStorage.setItem('cloud_type', process.env.CLOUD_TYPE || 'common');

let baseURL;
if (process.env.NODE_ENV === 'development') {
    // dev
    // baseURL = "http://172.25.0.30:18769";
    baseURL = baseUrl;
} else {
    // build
    baseURL = baseUrl;
}
const axios = Axios.create({
    baseURL,
});
// 原型链关联
axios.__proto__ = Axios
axios.defaults.timeout = 60000;

// http request 拦截器
axios.interceptors.request.use(
    (config) => {
        if (config.method === 'get') {
            // 如果是get请求，且params是数组类型如arr=[1,2]，则转换成url?ids=1&ids=2&ids=3
            config.paramsSerializer = function (params) {
                // return qs.stringify(params, { arrayFormat: 'repeat' })
                return qs.stringify(params, { indices: false });
            };
        }
        let token = cookieUse.getCookie('desig-token') || '';
        if (token && token !== '') {
            // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.Authorization = `${token}`;
        }
        // escapeRequestArgument(config);
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
);

// http response 拦截器
let tokenFlag = 0
axios.interceptors.response.use(
    (response) => {
        let token = cookieUse.getCookie('desig-token') || '';
        let _res = response;
        // escapeResponseArgument(response);
        if (response.data.code === -9599) {
            if(tokenFlag === 0 && router.history.current.path !== '/login' && !router.history.current.meta.login){
                Message.error({
                    content:response.data.message,
                    duration:2,
                })
                tokenFlag = 1
            }
            // 登录超时 跳转至登录页面
            setTimeout(() => {
                cookieUse.setCookie('desig-token', '', 0);
                cookieUse.setCookie('Authorization', '', 0);
                window.openLogin(router)
                // router.push({
                //     path: '/login',
                // });
              
            }, 2000);
            _res.data.message = '';
            return _res.data;
        } else if (response.data.code === -9597) {
            setTimeout(() => {
                
                cookieUse.setCookie('desig-token', '', 0);
                cookieUse.setCookie('Authorization', '', 0);
                // router.push({
                //     path: '/login',
                // });
                window.openLogin(router)
            }, 2000);
            return response.data;
        } else {
            if (
                sessionStorage.getItem('desig-rm') != 'true' ||
                sessionStorage.getItem('desig-rm') === undefined
            ) {
                cookieUse.setCookie('desig-token', token, 8);
                cookieUse.setCookie('Authorization', token, 8);
            }
            return response.data;
        }
    },
    (error) => {
        if (error.code === 'ECONNABORTED') {
            Message.error({
                content: '接口超时，请稍后重试......',
                duration: 2,
            });
            return Promise.reject('接口超时，请稍后重试......');
        }
        return Promise.reject(error); // 返回接口返回的错误信息
    }
);

export default axios;
